/*
 *  Copyright 2003 by Texas Instruments Incorporated.
 *  All rights reserved. Property of Texas Instruments Incorporated.
 *  Restricted rights to use, duplicate or disclose this code are
 *  granted through contract.
 *  
 */
/* "@(#) DDK 1.10.00.23 07-02-03 (ddk-b12)" */
#include <std.h>
#include <tsk.h>  
#include <sem.h>   
#include <gio.h>

#include <csl_dat.h>
#include <csl_cache.h>


#include <fvid.h>
#include <edc.h>
#include <vport.h>
#include <vportcap.h>
#include <vportdis.h>
#include <saa7105.h>
#include <tvp51xx.h>   

#include <evmdm642.h>

#include "colorbar.h"
#include "evmdm642_vcapparams.h"
#include "evmdm642_vdisparams.h"   

/* heap IDs defined in the BIOS configuration file */
extern Int EXTERNALHEAP;


/*
 * ======== main ========
 */
main()
{
    /******************************************************/
    /* open CSL DAT module for fast copy                  */
    /******************************************************/
    CSL_init();                                             
    CACHE_clean(CACHE_L2ALL, 0, 0);
    CACHE_setL2Mode(CACHE_256KCACHE);       
    CACHE_enableCaching(CACHE_EMIFA_CE00);
    CACHE_enableCaching(CACHE_EMIFA_CE01);
    DAT_open(DAT_CHAANY, DAT_PRI_LOW, DAT_OPEN_2D);

}

/*
 * ======== tskVideoLoopback ========
 * video loopback function.
 */
void tskVideoLoopback()
{
    Int status;
    Int frames = 0;
#ifdef _LOOPBACK
    FVID_Handle capChan;
    FVID_Frame *capFrameBuf; 
#ifdef _PIP
	FVID_Handle capChan2; 
    FVID_Frame *capFrameBuf2;
    VPORTCAP_Params EVMDM642_vCapParamsChan2 = EVMDM642_vCapParamsChan;   
#endif
#endif                
    /******************************************************/
    /* allocate both capture and display frame buffers    */
    /* in external heap memory                            */
    /******************************************************/
        EVMDM642_vCapParamsChan.segId = EXTERNALHEAP;
        
        EVMDM642_vCapParamsTVP51XX.hI2C = EVMDM642_I2C_hI2C;
		EVMDM642_vCapParamsTVP51XX.inputPort = 0;
    /******************************************************/
    /* initialization of capture driver                   */
    /******************************************************/
#ifdef _LOOPBACK
   // capChan = FVID_create("/VP0CAPTURE/A/0", 
    //        IOM_INPUT, &status, (Ptr)&EVMDM642_vCapParamsChan, NULL);

#ifdef _PIP
    EVMDM642_vCapParamsChan2.scale = VPORT_SCALING_ENABLE;//VPORT_SCALING_DISABLE;
    EVMDM642_vCapParamsChan2.fldOp = VPORT_FLDOP_FRAME;        
     //   EVMDM642_vCapParamsChan2.thrld >>=1;
    capChan2 = FVID_create("/VP1CAPTURE/A/1", 
            IOM_INPUT, &status, (Ptr)&EVMDM642_vCapParamsChan2, NULL);
#endif          

#endif          
   
#ifdef _LOOPBACK
  //  FVID_control(capChan, VPORT_CMD_EDC_BASE + EDC_CONFIG, 
   //     (Ptr)&EVMDM642_vCapParamsTVP51XX);
#ifdef _PIP
    EVMDM642_vCapParamsTVP51XX.aFmt = TVP51XX_AFMT_COMPOSITE;
    FVID_control(capChan2, VPORT_CMD_EDC_BASE+EDC_CONFIG, 
        (Ptr)&EVMDM642_vCapParamsTVP51XX);
#endif
#endif
    /******************************************************/
    /* start capture & display operation                  */    
    /******************************************************/
#ifdef _LOOPBACK
   // FVID_control(capChan, VPORT_CMD_START, NULL);
#ifdef _PIP
    FVID_control(capChan2, VPORT_CMD_START, NULL);
#endif    
#endif        
    /********************************************************/
    /* request a frame buffer from display & capture driver */
    /********************************************************/
#ifdef _LOOPBACK
   // FVID_alloc(capChan, &capFrameBuf);
#ifdef _PIP
    FVID_alloc(capChan2, &capFrameBuf2);
#endif      
#endif
    frames ++;

    while(1){/* loop forever */
#ifdef _LOOPBACK
       /* for(i = 0; i < numLines; i ++) {
            DAT_copy(capFrameBuf->frame.iFrm.y1 + i * capLinePitch, 
                     disFrameBuf->frame.iFrm.y1 + i * disLinePitch,
                     numPixels);
            DAT_copy(capFrameBuf->frame.iFrm.cb1 + i * (capLinePitch >> 1), 
                     disFrameBuf->frame.iFrm.cb1 + i * (disLinePitch >> 1),
                     numPixels>>1);

            DAT_copy(capFrameBuf->frame.iFrm.cr1 + i * (capLinePitch >> 1), 
                     disFrameBuf->frame.iFrm.cr1 + i * (disLinePitch >> 1),
                     numPixels>>1);
        }*/
#ifdef _PIP  
      /*  for(i = 0; i < (numLines>>1); i ++) {
            DAT_copy(capFrameBuf2->frame.iFrm.y1 + i * yPitch, 
                     disFrameBuf->frame.iFrm.y1 + i * disLinePitch
                     + (disLinePitch >> 1),
                     (numPixels>>1));
            DAT_copy(capFrameBuf2->frame.iFrm.cb1 + i * cPitch, 
                     disFrameBuf->frame.iFrm.cb1 + i * (disLinePitch >> 1)
                     + (disLinePitch >>2 ),
                     (numPixels >> 2));

            DAT_copy(capFrameBuf2->frame.iFrm.cr1 + i * cPitch, 
                     disFrameBuf->frame.iFrm.cr1 + i * (disLinePitch >> 1)
                     +(disLinePitch >> 2),
                     (numPixels >> 2));
        }*/
#endif
   //     FVID_exchange(capChan, &capFrameBuf);
#ifdef _PIP
        FVID_exchange(capChan2, &capFrameBuf2);
#endif
#else
       
#endif      
        frames ++;              
   }
}
